package com.example.liuxing.jdmall.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * Created by liuxing on 2017/7/19.
 */

public class UserDao { // 业务驱动代码


    private DbOpenHelp openHelp;

    public UserDao(Context c) {
        openHelp = new DbOpenHelp(c);
    }

    // 保存user
    public boolean saveUser(String name, String pwd) {
        SQLiteDatabase db = openHelp.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DbCons._NAME, name);
        values.put(DbCons._PWD, pwd);
        long insert = db.insert(DbCons.USER_TABLE, null, values);
        return insert != -1;
    }

    // 清空user
    public void clearUser() {
        SQLiteDatabase db = openHelp.getWritableDatabase();
        db.delete(DbCons.USER_TABLE, null, null);
    }

    public UserInfo aquireUser() {
        SQLiteDatabase db = openHelp.getWritableDatabase();
        Cursor cursor = db.query(DbCons.USER_TABLE,
                new String[]{DbCons._NAME, DbCons._PWD},
                null, null, null, null, null);
        if (cursor.moveToFirst()) {
            String name = cursor.getString(0);
            String pwd = cursor.getString(1);
            return new UserInfo(name, pwd);
        }
        return null;
    }

    public class UserInfo {
        public String name;
        public String pwd;

        public UserInfo(String name, String pwd) {
            this.name = name;
            this.pwd = pwd;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
